﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using UIDATALayer;
namespace VisionBaseLib
{
    public partial class VisionImage : UserControl
    {
        int n_Num = 1;
        public int 相机编号
        {
            set { n_Num = value; }
            get { return n_Num; }
        }
        public VisionImage()
        {
            InitializeComponent();
        }
        private UIDATALayer.UIDataAction<GaoNaVisionData> ac高纳VisionData = null;

        private void VisionImage_Load(object sender, EventArgs e)
        {
            VisionBaseMgr.GetInstance()._gNVisionDisp._ShowVisionImagedelegate += ShowVisionImage;
        }
        object o_ShowVisionImage = new object();
        PictureBox picture = new PictureBox();
        public void ShowVisionImage(int n_Index, Bitmap img)
        {
            if(ac高纳VisionData==null)
            {
                UIDATALayerHelper.RegistAction<GaoNaVisionData>(nameof(GaoNaVisionData), ShowVisionImage).Start();
                ac高纳VisionData = UIDATALayer.UIDATALayerHelper.GetRegistAction<GaoNaVisionData>(nameof(GaoNaVisionData));
                ac高纳VisionData.SetQueueNum(2);
            }
            if (n_Index == n_Num)
            {
                GaoNaVisionData data = new GaoNaVisionData();
                data.n_Index = n_Index;
                data.img = img;
                ac高纳VisionData.SetData(data);
                VisionBaseMgr.GetInstance().Function_SetImage(n_Num, data.img, data.img);
            }
        }
        public void ShowVisionImage(GaoNaVisionData data)
        {

            if (data.n_Index != n_Num)
                return;

            if (pic_Image.InvokeRequired)
            {
                pic_Image.BeginInvoke(new Action(() => ShowVisionImage(data)));
            }
            else
            {
                try
                {
                    lock (o_ShowVisionImage)
                    {
                        if (null != data.img)
                        {
                            data.img.Dispose();
                            pic_Image.Image = (Bitmap)data.img.Clone();
                            picture.Image = (Bitmap)data.img.Clone();
                        }

                    }
                }
                catch (Exception ex)
                {

                }
            }
        }
        private void pic_Image_DoubleClick(object sender, EventArgs e)
        {
            Form_VisionImage form_Vision = new Form_VisionImage();
            form_Vision.Text = this.Name;
            form_Vision.TopLevel = true;
            form_Vision.TopMost = true;
            form_Vision.Show();
            picture = form_Vision.pic_Image;
        }
        
    }
}
